#include "GBuffer.h"
#include "VertexBufferManager.h"
#include "IndexBuffer.h"
#include "../Utility/Renderer/GeometryLoader.h"

#ifdef _DEBUG
#include "../Utility/Misc/DebugOutput.h";
#endif

RenderTarget GBuffer::m_DepthRenderTarget;
RenderTarget GBuffer::m_DiffuseRenderTarget;
RenderTarget GBuffer::m_NormalRenderTarget;
RenderTarget GBuffer::m_SpecularRenderTarget;
RenderTarget GBuffer::m_SceneTarget;

unsigned int GBuffer::m_StartVertex;
unsigned int GBuffer::m_StartIndex;
unsigned int GBuffer::m_VertCount;
unsigned int GBuffer::m_PrimitiveCount;

void GBuffer::Initialize(unsigned int _width, unsigned int _height)
{
	//Create quad to render the final image to
	MeshDataPT* quad	= 0;
	quad				= GeometryLoader<MeshDataPT>::GetPlane();
	m_VertCount			= quad->vertices.size();
	m_StartVertex		= VertexBufferManager::m_PositionTexBuffer.AddVerts(&quad->vertices[0], m_VertCount);
	m_StartIndex		= IndexBuffer::AddIndices(&quad->indices[0], quad->indices.size());
	m_PrimitiveCount	= quad->indices.size()/3;

	//Create RenderTarget to render the final image to
	m_SceneTarget.Create(_width, _height, D3DFMT_A8R8G8B8);

	InitGBuffers(_width, _height);
}

bool GBuffer::InitGBuffers(unsigned int _width, unsigned int _height)
{
	m_DepthRenderTarget.Create(_width, _height, D3DFMT_R32F);
	m_DiffuseRenderTarget.Create(_width, _height, D3DFMT_A8R8G8B8);
	m_NormalRenderTarget.Create(_width, _height, D3DFMT_A2R10G10B10);
	m_SpecularRenderTarget.Create(_width, _height, D3DFMT_A2R10G10B10);

	#ifdef _DEBUG
	DebugOutput::Print(DebugOutput::RENDER_MSG, DebugOutput::SUCCESS_CREATE, "GBUFFERS");
	#endif
	
	return true;
}

void GBuffer::Render()
{
	m_DiffuseRenderTarget.ActivateTarget(0);
	//m_NormalRenderTarget.ActivateTarget(1);
	//m_SpecularRenderTarget.ActivateTarget(2);

	D3D9Renderer::device->Clear(0, 0, D3DCLEAR_TARGET, D3DCOLOR_XRGB(255,255,255),1,0);

	//m_SpecularRenderTarget.RevertTarget();
	//m_NormalRenderTarget.RevertTarget();
	m_DiffuseRenderTarget.RevertTarget();
	
}